package com.bestow.basic.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.bestow.basic.domain.FndCodingRuleDetails;
import com.bestow.basic.domain.FndCodingRules;
import com.bestow.basic.domain.HlsDocumentType;
import com.bestow.basic.mapper.FndCodingRuleDetailsMapper;
import com.bestow.basic.mapper.FndCodingRulesMapper;
import com.bestow.basic.mapper.HlsDocumentTypeMapper;
import com.bestow.common.utils.DateUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bestow.basic.mapper.FndCodingRuleValuesMapper;
import com.bestow.basic.domain.FndCodingRuleValues;
import com.bestow.basic.service.IFndCodingRuleValuesService;

/**
 * 编码规则值Service业务层处理
 *
 * @author liliu
 * @date 2022-09-03
 */
@Service
public class FndCodingRuleValuesServiceImpl implements IFndCodingRuleValuesService {
    @Autowired
    private FndCodingRuleValuesMapper fndCodingRuleValuesMapper;
    @Autowired
    private HlsDocumentTypeMapper hlsDocumentTypeMapper;
    @Autowired
    private FndCodingRulesMapper fndCodingRulesMapper;
    @Autowired
    private FndCodingRuleDetailsMapper fndCodingRuleDetailsMapper;

    /**
     * 查询编码规则值
     *
     * @param codingRuleId 编码规则值主键
     * @return 编码规则值
     */
    @Override
    public FndCodingRuleValues selectFndCodingRuleValuesByCodingRuleId(Long codingRuleId) {
        return fndCodingRuleValuesMapper.selectFndCodingRuleValuesByCodingRuleId(codingRuleId);
    }

    /**
     * 查询编码规则值列表
     *
     * @param fndCodingRuleValues 编码规则值
     * @return 编码规则值
     */
    @Override
    public List<FndCodingRuleValues> selectFndCodingRuleValuesList(FndCodingRuleValues fndCodingRuleValues) {
        return fndCodingRuleValuesMapper.selectFndCodingRuleValuesList(fndCodingRuleValues);
    }

    /**
     * 新增编码规则值
     *
     * @param fndCodingRuleValues 编码规则值
     * @return 结果
     */
    @Override
    public int insertFndCodingRuleValues(FndCodingRuleValues fndCodingRuleValues) {
        fndCodingRuleValues.setCreateTime(DateUtils.getNowDate());
        return fndCodingRuleValuesMapper.insertFndCodingRuleValues(fndCodingRuleValues);
    }

    /**
     * 修改编码规则值
     *
     * @param fndCodingRuleValues 编码规则值
     * @return 结果
     */
    @Override
    public int updateFndCodingRuleValues(FndCodingRuleValues fndCodingRuleValues) {
        fndCodingRuleValues.setUpdateTime(DateUtils.getNowDate());
        return fndCodingRuleValuesMapper.updateFndCodingRuleValues(fndCodingRuleValues);
    }

    /**
     * 批量删除编码规则值
     *
     * @param codingRuleIds 需要删除的编码规则值主键
     * @return 结果
     */
    @Override
    public int deleteFndCodingRuleValuesByCodingRuleIds(Long[] codingRuleIds) {
        return fndCodingRuleValuesMapper.deleteFndCodingRuleValuesByCodingRuleIds(codingRuleIds);
    }

    /**
     * 删除编码规则值信息
     *
     * @param codingRuleId 编码规则值主键
     * @return 结果
     */
    @Override
    public int deleteFndCodingRuleValuesByCodingRuleId(Long codingRuleId) {
        return fndCodingRuleValuesMapper.deleteFndCodingRuleValuesByCodingRuleId(codingRuleId);
    }

    /**
     * 获取编码规则
     *
     * @return 结果
     */
    @Override
    public String getCodeRuleValue(String documentCategory, String documentType, String businessType, Map<String, String> params) {
        HlsDocumentType type = new HlsDocumentType();
        type.setDocumentCategory(documentCategory);
        type.setDocumentType(documentType);
        type.setBusinessType(businessType);
        HlsDocumentType document = (HlsDocumentType) this.hlsDocumentTypeMapper.selectHlsDocumentTypeByDocumentType(documentType);
        Long codingRuleId = Long.valueOf(document.getCodingRuleId());
        String value = null;
        String segmentTypeValue = null;
        FndCodingRules codingRule = (FndCodingRules) this.fndCodingRulesMapper.selectFndCodingRulesByCodingRuleId(codingRuleId);
        if (codingRule == null) {
            return null;
        } else {
            FndCodingRuleDetails fndCodingRuleDetails = new FndCodingRuleDetails();
            fndCodingRuleDetails.setCodingRuleId(codingRule.getCodingRuleId());
            List<FndCodingRuleDetails> fndCodingRuleDetailsList = this.fndCodingRuleDetailsMapper.selectFndCodingRuleDetailsList(fndCodingRuleDetails);
            Iterator var = fndCodingRuleDetailsList.iterator();

            while (var.hasNext()) {
                FndCodingRuleDetails codingRuleDetails = (FndCodingRuleDetails) var.next();
                segmentTypeValue = this.getSegmentTypeValue(documentCategory, documentType, businessType, new Date(), codingRuleDetails.getSegmentType(), codingRule, params);
                if (value == null) {
                    value = segmentTypeValue;
                } else {
                    StringBuffer sb = new StringBuffer(value);
                    value = sb.append(segmentTypeValue).toString();
                }
            }

            return value;
        }
    }

    /**
     * 获取编码规则
     *
     * @return 结果
     */
    @Override
    public String getCodeRuleValue(String documentCategory, String documentType, String businessType, Date date, Map<String, String> params) {
        HlsDocumentType type = new HlsDocumentType();
        type.setDocumentCategory(documentCategory);
        type.setDocumentType(documentType);
        type.setBusinessType(businessType);
        HlsDocumentType document = (HlsDocumentType) this.hlsDocumentTypeMapper.selectHlsDocumentTypeByDocumentType(documentType);
        Long codingRuleId = Long.valueOf(document.getCodingRuleId());
        String value = null;
        String segmentTypeValue = null;
        FndCodingRules codingRule = (FndCodingRules) this.fndCodingRulesMapper.selectFndCodingRulesByCodingRuleId(codingRuleId);
        if (codingRule == null) {
            return null;
        } else {
            FndCodingRuleDetails fndCodingRuleDetails = new FndCodingRuleDetails();
            fndCodingRuleDetails.setCodingRuleId(codingRule.getCodingRuleId());
            List<FndCodingRuleDetails> fndCodingRuleDetailsList = this.fndCodingRuleDetailsMapper.selectFndCodingRuleDetailsList(fndCodingRuleDetails);
            Iterator var = fndCodingRuleDetailsList.iterator();

            while (var.hasNext()) {
                FndCodingRuleDetails codingRuleDetails = (FndCodingRuleDetails) var.next();
                segmentTypeValue = this.getSegmentTypeValue(documentCategory, documentType, businessType, date, codingRuleDetails.getSegmentType(), codingRule, params);
                if (value == null) {
                    value = segmentTypeValue;
                } else {
                    StringBuffer sb = new StringBuffer(value);
                    value = sb.append(segmentTypeValue).toString();
                }
            }

            return value;
        }
    }

    private String getSegmentTypeValue(String documentCategory, String documentType, String businessType, Date date, String segmentType, FndCodingRules codingRule, Map<String, String> params) {
        FndCodingRuleDetails codingRuleDetails = null;
        if ("SYS_01".equals(segmentType)) {
            codingRuleDetails = this.fndCodingRuleDetailsMapper.selectByUk(segmentType, codingRule.getCodingRuleId());
            return codingRuleDetails.getSegmentValue();
        } else if ("SYS_02".equals(segmentType)) {
            codingRuleDetails = this.fndCodingRuleDetailsMapper.selectByUk(segmentType, codingRule.getCodingRuleId());
            Date currentTime = new Date();
            String format = codingRuleDetails.getDateFormat().replace("D", "d");
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(currentTime);
        } else if ("SYS_03".equals(segmentType)) {
            String value = this.getSeqValue(segmentType, codingRule, date);
            return value;
        } else if ("SYS_04".equals(segmentType)) {
            return documentCategory;
        } else if ("SYS_05".equals(segmentType)) {
            return documentType;
        } else {
            return "SYS_06".equals(segmentType) ? businessType : (String) params.get(segmentType);
        }
    }

    private String getSeqValue(String segmentType, FndCodingRules codingRule, Date date) {
        FndCodingRuleValues codingRulevalue = null;
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }

        int month = cal.get(2) + 1;
        int year = cal.get(1);
        FndCodingRuleDetails codingRuleDetails = this.fndCodingRuleDetailsMapper.selectByUk(segmentType, codingRule.getCodingRuleId());
        if ("MONTH".equals(codingRule.getResetFrequence())) {
            codingRulevalue = this.fndCodingRuleValuesMapper.selectByUyKey(codingRule.getCodingRuleId(), Integer.toString(month), new Long((long) year));
        } else if ("YEAR".equals(codingRule.getResetFrequence())) {
            codingRulevalue = this.fndCodingRuleValuesMapper.selectByUyKey(codingRule.getCodingRuleId(), (String) null, new Long((long) year));
        } else {
            codingRulevalue = this.fndCodingRuleValuesMapper.selectByUyKey(codingRule.getCodingRuleId(), (String) null, (Long) null);
        }

        Long seqValue;
        String value;
        if (codingRulevalue == null) {
            seqValue = codingRuleDetails.getStartValue() + codingRuleDetails.getIncremental();

            for (value = seqValue.toString(); (long) value.length() < codingRuleDetails.getLength(); value = "0" + value) {
            }

            FndCodingRuleValues codingRuleValues = new FndCodingRuleValues();
            if ("MONTH".equals(codingRule.getResetFrequence())) {
                codingRuleValues.setCodingRuleId(codingRule.getCodingRuleId());
                codingRuleValues.setMonth(Integer.toString(month));
                codingRuleValues.setYear(year);
                codingRuleValues.setCurrentValue(seqValue.intValue());
            } else if ("YEAR".equals(codingRule.getResetFrequence())) {
                codingRuleValues.setCodingRuleId(codingRule.getCodingRuleId());
                codingRuleValues.setYear(year);
                codingRuleValues.setCurrentValue(seqValue.intValue());
            } else {
                codingRuleValues.setCodingRuleId(codingRule.getCodingRuleId());
                codingRuleValues.setCurrentValue(seqValue.intValue());
            }
            fndCodingRuleValuesMapper.insertFndCodingRuleValues(codingRuleValues);
            return value;
        } else {
            seqValue = codingRulevalue.getCurrentValue() + codingRuleDetails.getIncremental();

            for (value = seqValue.toString(); (long) value.length() < codingRuleDetails.getLength(); value = "0" + value) {
            }

            if ("MONTH".equals(codingRule.getResetFrequence())) {
                this.fndCodingRuleValuesMapper.updateByUyKey(codingRule.getCodingRuleId(), Integer.toString(month), new Long((long) year), seqValue);
            } else if ("YEAR".equals(codingRule.getResetFrequence())) {
                this.fndCodingRuleValuesMapper.updateByUyKey(codingRule.getCodingRuleId(), (String) null, new Long((long) year), seqValue);
            } else {
                this.fndCodingRuleValuesMapper.updateByUyKey(codingRule.getCodingRuleId(), (String) null, (Long) null, seqValue);
            }

            return value;
        }
    }
}
